package com.hongxun.budget.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.tk.SetMainActivity;
import com.hongxun.budget.ui.activity.ys.CheckSetMainActivity;
import com.previewlibrary.GPreviewActivity;


/**
 * Created by wangbing on 2017/12/6.
 * 初始化操作
 */

public class AppInit implements Application.ActivityLifecycleCallbacks {

    private Application application;
    private static AppInit appInit;


    private AppInit(Application application) {
        this.application = application;
        application.registerActivityLifecycleCallbacks(this);
    }

    public static void getInstance(Application application) {
        appInit = new AppInit(application);
    }

    @Override
    public void onActivityCreated(final Activity activity, Bundle savedInstanceState) {
        if (activity.findViewById(R.id.center_title) != null && activity.getTitle() != null && !activity.getTitle().equals("")) {
            ((TextView) activity.findViewById(R.id.center_title)).setText(activity.getTitle());
            activity.findViewById(R.id.back_btn).setVisibility(View.VISIBLE);
            activity.findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (activity instanceof GPreviewActivity) {
                        ((GPreviewActivity) activity).transformOut();
                        activity.finish();
                    } else if (activity instanceof SetMainActivity) {
                        ((SetMainActivity) activity).finishSave();
                    }else if (activity instanceof CheckSetMainActivity) {
                        ((CheckSetMainActivity) activity).finishSave();
                    } else
                        activity.finish();
                }
            });
        } else if (activity.findViewById(R.id.center_spinner) != null) {
            activity.findViewById(R.id.back_btn).setVisibility(View.VISIBLE);
            activity.findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (activity instanceof GPreviewActivity)
                        ((GPreviewActivity) activity).transformOut();
                    activity.finish();
                }
            });
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
